<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增部门')"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-dept-add">
        <input id="treeId" name="parentId" th:value="${dept.deptId}" type="hidden"/>
        <input id="parentType" th:value="${dept.type}" type="hidden"/>
        <input id="parentLevel" th:value="${dept.level}" type="hidden"/>

        <div class="form-group">
            <label class="col-sm-3 control-label" for="treeName">上级部门：</label>
            <div class="col-sm-8">
                <div class="input-group">
                    <input class="form-control" id="treeName" onclick="selectDeptTree();" readonly
                           th:value="${dept.deptName}"
                           type="text">
                    <span class="input-group-addon"><i class="fa fa-search"></i></span>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required" for="deptName">部门名称：</label>
            <div class="col-sm-8">
                <input class="form-control" id="deptName" name="deptName" required type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label" for="deptType">部门类型：</label>
            <div class="col-sm-8">
                <select class="form-control m-b" id="deptType" name="type" required
                        th:with="deptType=${@dict.getType('dept_type')}">
                    <option th:each="dict : ${deptType}" th:field="*{dept.type}" th:text="${dict.dictLabel}"
                            th:value="${dict.dictValue}"></option>
                </select>
            </div>
        </div>
        <input hidden="hidden" id="deptLevel" name="level" value="">
        <div class="form-group" hidden>
            <label class="col-sm-3 control-label is-required" for="order">显示排序：</label>
            <div class="col-sm-8">
                <input class="form-control" id="order" name="orderNum" required type="text" value="0">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required" for="leader">负责人：</label>
            <div class="col-sm-8">
                <input class="form-control" id="leader" name="leader" required type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label" for="status">部门状态：</label>
            <div class="col-sm-8">
                <div class="radio-box" th:each="dict : ${@dict.getType('sys_normal_disable')}">
                    <input id="status" name="status" th:checked="${dict.default}" th:id="${dict.dictCode}"
                           th:value="${dict.dictValue}"
                           type="radio">
                    <label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<script type="text/javascript">
    let prefix = ctx + "system/dept";

    $("#form-dept-add").validate({
        onkeyup: false,
        rules: {
            deptName: {
                remote: {
                    url: prefix + "/checkDeptNameUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "parentId": function () {
                            return $("input[name='parentId']").val();
                        },
                        "deptName": function () {
                            return $.common.trim($("#deptName").val());
                        }
                    },
                    dataFilter: function (data, type) {
                        return $.validate.unique(data);
                    }
                }
            },
            orderNum: {
                digits: true
            },
            /*email:{
                email:true,
            },*/
            phone: {
                isPhone: true,
            },
        },
        messages: {
            "deptName": {
                remote: "部门已经存在"
            }
        },
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            $("#deptType").removeAttr("disabled");
            $.operate.save(prefix + "/add", $('#form-dept-add').serialize());
        }
    }

    /*部门管理-新增-选择父部门树*/
    function selectDeptTree() {
        let options = {
            title: '部门选择',
            width: "380",
            url: prefix + "/selectDeptTree/" + $("#treeId").val(),
            callBack: doSubmit
        };
        $.modal.openOptions(options);
    }

    function doSubmit(index, layero) {
        let body = layer.getChildFrame('body', index);
        let deptLevel = Number(body.find('#deptLevel').val()) + 1;
        if (deptLevel > 5) {
            $.modal.alertError("上级单元不能为五级单元");
            return;
        }
        $("#deptLevel").val(deptLevel);
        let deptType = body.find('#deptType').val();
        $("#deptType").val(deptType);
        $("#treeId").val(body.find('#treeId').val());
        $("#parentType").val(body.find('#deptType').val());
        $("#treeName").val(body.find('#treeName').val());
        //检测部门类型框是否可用
        checkTypeInputDisabled();
        layer.close(index);
    }

    $(function () {
        $("#deptLevel").attr("value", Number($("#parentLevel").val()) + 1 + "");
    });

    $(checkTypeInputDisabled);

    /**
     * 检测部门类型框是否可用
     */
    function checkTypeInputDisabled() {
        let parentType = $("#parentType").val();
        let deptType = $("#deptType")
        if (parentType === "0" || parentType === "1") {
            deptType.removeAttr("disabled");
            if (parentType === "1") {
                $("#deptType option[value='1']").remove();
            } else if (parentType === "0") {
                let x = $("#deptType option[value='1']").html();
                if (!x) {
                    deptType.append("<option value='1'>综合</option>");
                }
            }
        } else {
            deptType.attr("disabled", "disabled");
        }
    }
</script>
</body>
</html>
